#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include"block_queue.hpp"
#include"task.hpp"
#include<time.h>

void* producer(void* args)
{
    block_queue<task>* bq = static_cast<block_queue<task>*>(args);

    while (true)
    {
        int x = rand() % 10;
        int y = rand() % 10;
        char op = opers[rand() % opers.size()];

        task t(x, y, op);
        bq->push(t);

        std::cout << t.getTask() << std::endl;
        //sleep(1);

    }
}

void* consumer(void* args)
{
    block_queue<task>* bq = static_cast<block_queue<task>*>(args);
    while (true)
    {
        task tmp = bq->pop();
        tmp();
        std::cout << tmp.getResult() << std::endl;
        sleep(1);
    }
}


int main()
{
    srand(time(nullptr));
    block_queue<task>* bq = new block_queue<task>();
    pthread_t ptid, ctid;
    pthread_create(&ptid, nullptr, producer, bq);
    pthread_create(&ctid, nullptr, consumer, bq);

    pthread_join(ptid, nullptr);
    pthread_join(ctid, nullptr);
    return 0;
}